/*此方法库中的方法都是需要在附件列表加载完成后才能加载的*/


/*为附件列表添加右键菜单*/
function bing_eattl_fun(p_id){	
	//先渲染附件列表面板
	$('#panel_attlist_'+p_id).panel(); 
	
	//再渲染附件列表名称
	$('.att_btn').linkbutton({
		plain:true
	});
	//然后绑定列表右键事件
	$(document).contextmenu(function(e){return false;});
	$(".eattl").bind('contextmenu',function(e){
		e.preventDefault();
		var att_m =$(this).find("#att_m").val();
		var att_id =$(this).find("#att_id").val();
		var att_name =$(this).find("#att_name").val();
		var att_size =$(this).find("#att_size").val();
		var run_id =$(this).find("#run_id").val();
		var ps_id =$(this).find("#ps_id").val();
		//$('#eattmm').children("div:eq(1)").find(".menu-text").text(v);

		//var suffix_array = [".doc",".docx",".xls",".xlsx"];
		//var get_suffix = get_suffix(att_name);
		//var fileExt=(/[.]/.exec(att_name)) ? /[^.]+$/.exec(att_name.toLowerCase()) : '';  
		//alert(get_suffix);
		//var whether_there = $.inArray(fileExt,suffix_array);
		
		$('#eattmm').children("div:eq(1)").attr("onclick","att_down('"+att_m+"','"+att_id+"','"+att_name+"')");
		$('#eattmm').children("div:eq(2)").attr("onclick","att_save('"+att_m+"','"+att_id+"','"+att_name+"')");
		
		var point = att_name.lastIndexOf(".");  
		var type = att_name.substr(point);
			type = type.toLowerCase();
		if(type==".doc"||type==".docx"||type==".xls"||type==".xlsx"){  
			$('#eattmm').children("div:eq(3)").attr("onclick","att_read('0','"+att_m+"','"+att_id+"','"+att_name+"')");
			$('#eattmm').children("div:eq(3)").css("display","block");
			if(att_m == 'workflow'  && ps_id > 0){
				$('#eattmm').children("div:eq(4)").attr("onclick","att_read('1','"+att_m+"','"+att_id+"','"+att_name+"')");
				$('#eattmm').children("div:eq(4)").css("display","block");
			}else{
				$('#eattmm').children("div:eq(4)").css("display","none");
				$('#eattmm').children("div:eq(5)").css("display","none");
			}
		}else{
			$('#eattmm').children("div:eq(3)").css("display","none");
			$('#eattmm').children("div:eq(4)").css("display","none");
			$('#eattmm').children("div:eq(5)").css("display","none");
		}
		if(att_m == 'workflow' && ps_id > 0){
			$('#eattmm').children("div:eq(5)").attr("onclick","att_del('"+run_id+"','"+ps_id+"','"+att_m+"','"+att_id+"','"+att_name+"','"+att_size+"')");
			$('#eattmm').children("div:eq(5)").css("display","block");
		}
		

		//$('#eattmm').val(v);
		
		$('#eattmm').menu('show', {
			left: e.pageX,
			top: e.pageY
		});
	});
	return false;
}

/*转存附件*/
function att_save(m,att_id,att_name){
	att_name = encodeURIComponent(att_name);
	$.post(PROJECT_PATH+'attachment/att_exist',{m:m,att_id:att_id,att_name:att_name},function(data,status){
		//alert("Data: " + data + "\nStatus: " + status);
		if(data.status){
			var url = 'file_cabinets/select_filebox_in/'+m+'/'+att_id+'/'+att_name;
			dialog_public('转存至个人文件柜',url);
		}else{
			$.messager.alert('警告','文件不存在，可能已被删除！');
		}
	},'JSON');
	
}

/*下载附件*/
function att_down(m,att_id,att_name){
	var att_name = encodeURIComponent(att_name);
	$.post(PROJECT_PATH+'attachment/att_exist',{m:m,att_id:att_id,att_name:att_name},function(data,status){
		if(data.status){
			//var url = pro_path+'attachment/att_down?m='+m+'&att_id='+att_id+'&att_name='+att_name;
			//var str = "<iframe height='0' width='0' frameborder='0'  src=" + url + " ></iframe>";  
			//$("#attachmentIFrame").html(str);
			var content = "<form id='attachmentform' action='"+PROJECT_PATH+"attachment/att_down' method='POST'>";
			content += "<input type='hidden' name='m' value='"+m+"'>";
			content += "<input type='hidden' name='att_id' value='"+att_id+"'>";
			content += "<input type='hidden' name='att_name' value='"+att_name+"'></form>";
			$("#attachmentIFrame").empty().append(content);
			$("#attachmentform").submit();
		}else{
			$.messager.alert('警告','文件不存在，可能已被删除！');
		}
	},'JSON');
}


/*阅读或编辑附件*/
function att_read(f,m,att_id,att_name){
	var att_name_encode = encodeURIComponent(att_name);

	$.post(PROJECT_PATH+'attachment/att_exist',{m:m,att_id:att_id,att_name:att_name_encode},function(data,status){
		//alert("Data: " + data + "\nStatus: " + status);
		if(data.status){
			if(user_agent == 'Internet Explorer'){
				$('#window_custom_box').window({
					title: att_name,
					method:'post',
					collapsible:false,
					minimizable:false,
					width: 400,
					height: 200,
					closed: false,
					cache: false,
					href: PROJECT_PATH+'attachment/att_read',
					draggable:false,
					resizable:false,
					modal: true,
					maximizable:false,
					maximized:true,
					queryParams: {
						f:f,
						m:m,
						att_id:att_id,
						att_name:att_name_encode
					},
					onClose:function(){
						document.all.oframe.close();
					}
				});
			}else{
				$.messager.alert('警告',user_agent+'：只有IE浏览器才支持阅读或编辑功能!','info');	
			}
		}else{
			$.messager.alert('警告','文件不存在，可能已被删除！');
		}
	},'JSON');
}

/*-工作流中删除附件-*/
function att_del(run_id,ps_id,m,att_id,att_name,att_size){
	$.messager.confirm('提示', '确定要删除选中的附件吗?', function(r){
		if(r){ 
			//删除数据库中对应的附件
			$.post(PROJECT_PATH+'workflow/flow_att_del',{
				run_id:run_id,
				m:m,
				att_id:att_id,
				att_name:att_name,
				att_size:att_size
			},function(result){
				if(result.success){
					var student = new Object(); 
					student.daterand = att_id; 
					student.name = att_name; 
					
					//删除磁盘上的附件
					$.post(PROJECT_PATH+'attachment/upload_del',{
						data:JSON.stringify(student),
						m:'workflow'
					},function(data,status){
						if(data.status){
							//alert("ok");
						}
					},'JSON');
					
					
					$("#att_list_box_"+run_id).load(PROJECT_PATH+'workflow/flow_transact',{
						id:run_id,
						ps_id:ps_id,
						is_v:0
					},function(responseTxt,statusTxt,xhr){
						if(statusTxt=="success"){
							bing_eattl_fun('workflow'+run_id);//重新渲染附件列表和右键事件
							
						}
						if(statusTxt=="error")
							alert("Error: "+xhr.status+": "+xhr.statusText);
					});
					/*
					//只要数据库中对应的附件删除成功，不管磁盘上的文件是否存在或是否成功删除都刷新tab面板
					var tab = $('#main-tab').tabs('getSelected');       
					$("#main-tab").tabs('update',{  
						tab:tab,  
						options:{  
							id:'workflow_'+run_id,
							title:tab.title,  
							href:pro_path+'workflow/contentbox/'+run_id+'/'+ps_id 
						}  
					});
					*/
				}else{
					$.messager.alert('错误',result.errorMsg,'error');
				}
			},'json');
		}
	});
}

//转存附件到文件柜函数
/*
function att_save(m,att_id,att_name){
	$('#dialog_custom_box').dialog({
			title: '转存至个人文件柜',
			width: 600,
			height: 500,
			closed: false,
			cache: false,
			href: pro_path+'file_cabinets/select_filebox_in/'+m+'/'+att_id+'/'+encodeURIComponent(att_name),
			modal: true,
			onClose:function(){
				
			}
		});

}
*/